package com.cloud.open.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cloud.common.bean.PageInfo;
import com.cloud.open.dao.ResourceDao;
import com.cloud.open.dto.ResourceQueryParamDTO;
import com.cloud.open.entity.Resource;
import com.cloud.open.service.AppAuthorizationService;
import com.cloud.open.service.ResourceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import java.io.Serializable;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 *  授权资源服务实现类
 * </p>
 *
 * @author wenjin
 * @since 2019-09-25
 */
@Service
public class ResourceServiceImpl extends ServiceImpl<ResourceDao, Resource> implements ResourceService {

    @Autowired
    private AppAuthorizationService appAuthorizationService;

    @Override
    public boolean checkNameExist(String name, Integer typeId, Integer ignoreId) {
        QueryWrapper<Resource> wrapper = new QueryWrapper();
        wrapper.eq(Resource.NAME, name).eq(Resource.TYPE_ID, typeId);
        if (ignoreId != null) {
            wrapper.ne(Resource.ID, ignoreId);
        }
        List<Resource> resourceList = list(wrapper);
        if (CollectionUtils.isEmpty(resourceList)) {
            return false;
        }
        return true;
    }

    @Override
    public boolean checkAccessUriExist(String accessUri, Integer typeId, Integer ignoreId) {
        QueryWrapper<Resource> wrapper = new QueryWrapper();
        wrapper.eq(Resource.ACCESS_URI, accessUri).eq(Resource.TYPE_ID, typeId);
        if (ignoreId != null) {
            wrapper.ne(Resource.ID, ignoreId);
        }
        List<Resource> resourceList = list(wrapper);
        if (CollectionUtils.isEmpty(resourceList)) {
            return false;
        }
        return true;
    }

    @Override
    public PageInfo<Resource> getPage(ResourceQueryParamDTO paramDTO) {
        QueryWrapper<Resource> wrapper = new QueryWrapper();
        Integer typeId = paramDTO.getTypeId();
        if (typeId != null) {
            wrapper.eq(Resource.TYPE_ID, typeId);
        }
        String searchKey = paramDTO.getSearchKey();
        if (!StringUtils.isEmpty(searchKey)) {
            wrapper.and(queryWrapper -> {
                queryWrapper.like(Resource.NAME, searchKey);
                queryWrapper.or();
                queryWrapper.like(Resource.ACCESS_URI, searchKey);
            });

        }
        IPage page = page(new Page(paramDTO.getPageNo(), paramDTO.getPageSize()), wrapper);
        return new PageInfo(page);
    }

    @Override
    public List<Resource> getByTypeId(Serializable typeId) {
        QueryWrapper<Resource> wrapper = new QueryWrapper();
        wrapper.eq(Resource.TYPE_ID, typeId);
        return list(wrapper);
    }

    @Override
    public List<Resource> getAll() {
        QueryWrapper<Resource> wrapper = new QueryWrapper();
        wrapper.orderByAsc(Resource.TYPE_ID);
        return list(wrapper);
    }

    @Override
    public boolean deleteByTypeId(Serializable typeId) {
        List<Resource> resourceList = getByTypeId(typeId);
        if (CollectionUtils.isEmpty(resourceList)) {
            return true;
        }
        List<Integer> resourceIds = resourceList.stream().map(Resource::getId).collect(Collectors.toList());
        appAuthorizationService.deleteByResourceIds(resourceIds);
        return removeByIds(resourceIds);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean removeById(Serializable id) {
        List ids = Arrays.asList(id);
        appAuthorizationService.deleteByResourceIds(ids);
        return super.removeById(id);
    }
}
